//
//  DetailImageCell.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/20.
//

import UIKit

extension DetailImageCell: CTTableViewCellInitProtocol { }

class DetailImageCell: UITableViewCell {
    
    private var model: DataModel?
    
    private let imageV0 = UIImageView()
    private let imageV1 = UIImageView()
    private let imageV2 = UIImageView()
    
    typealias  ParamClosure = (_ idx: Int) -> Void
    var imgAction: ParamClosure?
    

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupLayout()
        setupStyle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Reactive
extension DetailImageCell: ViewModelConfigurable {
    
    func configure(_ model: DataModel) {
        self.model = model
        let placeholder = UIImage(named: "placeholder_empty")
        imageV0.isHidden = true
        imageV1.isHidden = true
        imageV2.isHidden = true
        let imgCount = model.imgUrls.count
        if imgCount == 1 {
            imageV0.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
        } else if imgCount == 2 {
            imageV0.isHidden = false
            imageV1.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
            imageV1.kf.setImage(with: model.imgUrls[1].asUrl, placeholder: placeholder)
        } else if imgCount >= 3 {
            imageV0.isHidden = false
            imageV1.isHidden = false
            imageV2.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
            imageV1.kf.setImage(with: model.imgUrls[1].asUrl, placeholder: placeholder)
            imageV2.kf.setImage(with: model.imgUrls[2].asUrl, placeholder: placeholder)
        }
        updateLayout(model)
    }

    @objc func clickImg(sender : UITapGestureRecognizer) {
        let tapview = sender.view
        guard let flag = tapview?.tag else { return }
        let idx = flag-100
        imgAction?(idx)
    }
}

// MARK: - UI Setup
private extension DetailImageCell {
    func setupLayout() {
        contentView.addSubview(imageV0)
        contentView.addSubview(imageV1)
        contentView.addSubview(imageV2)
        
        imageV0.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        imageV1.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        imageV2.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
    }
    
    func updateLayout(_ model: DataModel) {
        let imgCount = model.imgUrls.count
        let imgItemH = kScreenWid-2*10.fitR
        if imgCount == 1 {
            imageV0.snp.remakeConstraints { (m) in
                m.leading.top.trailing.bottom.equalToSuperview().inset(10.fitR)
            }
        } else if imgCount == 2 {
            imageV0.snp.remakeConstraints { (m) in
                m.leading.top.trailing.equalToSuperview().inset(10.fitR)
                m.height.equalTo(imgItemH)
            }
            imageV1.snp.remakeConstraints { (m) in
                m.leading.trailing.equalToSuperview().inset(10.fitR)
                m.top.equalTo(imageV0.snp.bottom).offset(10.fitR)
                m.height.equalTo(imgItemH)
            }
        } else if imgCount >= 3 {
            imageV0.snp.remakeConstraints { (m) in
                m.leading.top.trailing.equalToSuperview().inset(10.fitR)
                m.height.equalTo(imgItemH)
            }
            imageV1.snp.remakeConstraints { (m) in
                m.leading.trailing.equalToSuperview().inset(10.fitR)
                m.top.equalTo(imageV0.snp.bottom).offset(10.fitR)
                m.height.equalTo(imgItemH)
            }
            imageV2.snp.remakeConstraints { (m) in
                m.leading.trailing.equalToSuperview().inset(10.fitR)
                m.top.equalTo(imageV1.snp.bottom).offset(10.fitR)
                m.height.equalTo(imgItemH)
            }
        }
    }
    
    func setupStyle() {
        selectionStyle = .none
        backgroundColor = .color(.white)
        imageV0.do { (v) in
            v.backgroundColor = .color(.white)
            v.contentMode = .scaleAspectFill
            v.clipsToBounds = true
            v.isHidden = true
            v.isUserInteractionEnabled = true
            v.tag = 100
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickImg(sender:))))
        }
        imageV1.do { (v) in
            v.backgroundColor = .color(.white)
            v.contentMode = .scaleAspectFill
            v.clipsToBounds = true
            v.isHidden = true
            v.isUserInteractionEnabled = true
            v.tag = 101
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickImg(sender:))))
        }
        imageV2.do { (v) in
            v.backgroundColor = .color(.white)
            v.contentMode = .scaleAspectFill
            v.clipsToBounds = true
            v.isHidden = true
            v.isUserInteractionEnabled = true
            v.tag = 102
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickImg(sender:))))
        }
    }
}

